package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "动态相关接口")
@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @ApiOperation("发布动态")
    @PostMapping
    public ResponseEntity postMovements(MovementVo movementVo, MultipartFile[] imageContent) {
        movementService.postMovements(movementVo, imageContent);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("通过用户Id查看动态")
    @GetMapping("/all")
    public ResponseEntity getAllMovementsById(@RequestParam(defaultValue = "1") Long page,
                                          @RequestParam(name = "pagesize", defaultValue = "10") Long pageSize,
                                          @RequestParam Long userId) {
        PageResult<MovementsVo> pageResult = movementService.findByUserId(page, pageSize, userId);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("获取好友动态")
    @GetMapping
    public ResponseEntity getFriendMovements(@RequestParam(defaultValue = "1") Long page,
                                             @RequestParam(name = "pageSize", defaultValue = "10")Long pageSize) {
        PageResult pageResult = movementService.getFriendMovements(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("查看推荐动态")
    @GetMapping("/recommend")
    public ResponseEntity getRecommendMovements(@RequestParam(defaultValue = "1") Long page,
                                    @RequestParam(defaultValue = "10", name = "pagesize") Long pageSize) {
        PageResult pageResult = movementService.getRecommendMovements(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("通过id获取动态")
    @GetMapping("/{id}")
    public ResponseEntity getMovementById(@PathVariable String id) {
        MovementsVo vo = movementService.getMovementById(id);
        return ResponseEntity.ok(vo);
    }

    @ApiOperation("动态点赞")
    @GetMapping("/{id}/like")
    public ResponseEntity movementLike(@PathVariable("id") String movementId) {
        Integer likeCount = movementService.movementLike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    @ApiOperation("动态取消点赞")
    @GetMapping("/{id}/dislike")
    public ResponseEntity movementDislike(@PathVariable("id") String movementId) {
        Integer likeCount = movementService.movementDislike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    @ApiOperation("动态喜欢")
    @GetMapping("/{id}/love")
    public ResponseEntity movementLove(@PathVariable("id") String movementId) {
        Integer loveCount = movementService.movementLove(movementId);
        return ResponseEntity.ok(loveCount);
    }

    @ApiOperation("动态不喜欢")
    @GetMapping("/{id}/unlove")
    public ResponseEntity movementUnlove(@PathVariable("id") String movementId) {
        Integer loveCount = movementService.movementUnlove(movementId);
        return ResponseEntity.ok(loveCount);
    }

    @ApiOperation("谁看过我")
    @GetMapping("/visitors")
    public ResponseEntity findLast4Visitors() {
        List<VisitorsVo> vos = movementService.findLast4Visitors();
        return ResponseEntity.ok(vos);
    }


}
